#  Copyright (c) 1997-2010
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
#  http://www.polymake.de
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-----------------------------------------------------------------------------
#  $Project: polymake $$Id: Makefile 10086 2011-03-24 10:17:01Z herr $

#
#  Building and installation
#
.PHONY: all compile all-java compile-java-jars compile-java-native install install-arch install-shared \
	install-arch-prep docs clean clean-bin clean-java distclean TAGS RTAGS test

.shell := $(firstword $(wildcard /bin/bash /usr/bin/bash /usr/local/bin/bash))
ifdef .shell
  SHELL := ${.shell}
endif

ProjectTop := ${CURDIR}
include support/utils.make
ifndef BuildDir
  BuildDir := $(call _LocateBuildDir)
  ifndef BuildDir
    $(error CONFIGURATION ERROR)
  endif
endif

### load configuration
include ${BuildDir}/conf.make

### default target
all : compile $(if $(filter n,${JavaBuild}),, compile-java-native)

Apps := $(notdir $(filter-out $(call _get_no_export,apps/*,broken|test), $(wildcard apps/*)))

define _MakeApp
	$(MAKE) -C ${BuildDir}/apps/$(1) AppName=$(1) $(2)

endef
define _MakeApps
	$(MAKE) -C ${BuildDir}/lib $(1)
	$(foreach a,${Apps},$(call _MakeApp,$a,$(1)))
endef
define _InstallSubdir
${PERL} ${INSTALL_PL} -m ${DirMask} -U $(2) $(1) ${InstallTop}/$(1)

endef
define _InstallSubdirRenamed
${PERL} ${INSTALL_PL} -m ${DirMask} -U $(3) $(1) ${InstallTop}/$(2)

endef
define _CreateDir
${PERL} ${INSTALL_PL} -d -m ${DirMask} $(1)

endef

CallPolymake := $(if ${ARCHFLAGS},$(if $(findstring arch,${PERL}),,arch ${ARCHFLAGS})) ${PERL} perl/polymake

define _ConfigureJava
${CallPolymake} --iscript configure_java

endef
ifdef ANT
  define _MakeJava
	${ANT} -q -f support/polymake_build.xml -Dpolymake.top=${ProjectTop} -Djavaview.path=${JavaViewPath} $(1)

  endef
else
  define _MakeJava
	@echo ant utility was not found or of an ancient version.
	@echo Please repeat the configure step if you want to recompile the java modules.
	false

  endef
endif

_MakeAppsJavaNative = $(foreach a,${Apps},$(if $(wildcard apps/$a/src/native),$(call _MakeApp,$a,$(1)-native)))

compile:
	@+$(call _MakeApps,compile)

ifeq (${JavaBuild},n)
  all-java compile-java-native compile-java-jars clean-java :
	@echo Build architecture ${Arch} was configured --without-java or ant utility was not found.
	@echo Please repeat the configure step if you want to recompile the java modules.
	false
else
  ifdef JavaViewPath
    compile-java-jars:
	$(call _MakeJava,all)
  else
    compile-java-jars : compile
	$(call _ConfigureJava)
	@$(MAKE) --no-print-directory $@
  endif

  ifdef JNI_HEADERS
    compile-java-native : $(if $(filter y,${JavaBuild}),compile-java-jars)
	@+$(call _MakeAppsJavaNative,compile)
    all-java : compile-java-jars compile-java-native
  else
    all-java compile-java-native : compile
	$(call _ConfigureJava)
	@$(MAKE) --no-print-directory $@
  endif

  clean-java:
	$(call _MakeJava,clean)
endif

### installation

# scripts in support/ which should not be copied to the final installation location
InstHelpers := configure.pl convert_main_script find-provides find-requires

define _InstallShared
	$(foreach d, perllib rules scripts, $(if $(wildcard $(1)/$d), $(call _InstallSubdir,$(1)/$d)))
endef

install : install-shared install-arch

install-arch : all install-arch-prep
	@+$(call _MakeApps,install)
	$(if $(filter n,${JavaBuild}),,@+$(call _MakeAppsJavaNative,install))
	@case "$$(uname -s)" in \
	 *linux*|*Linux*) \
	   for d in linux32 linux64; do \
	     ${PERL} ${INSTALL_PL} -m ${DirMask} -U external/jreality/jni/$$d ${InstallArch}/lib/jreality/jni/$$d; \
	   done; \
	   ;; \
         Darwin*|darwin*) \
	   ${PERL} ${INSTALL_PL} -m ${DirMask} -U external/jreality/jni/macosx ${InstallArch}/lib/jreality/jni/macosx; \
	   ;; \
	 esac
	[ -d ${InstallBin} ] || $(call _CreateDir,${InstallBin})
	${PERL} support/convert_main_script --bindir ${InstallBin} \
		$(if ${PREFIX},--prefix ${PREFIX}) $(if ${FinkBase},--perllib ${FinkBase}/lib/perl5) \
		InstallTop=${FinalInstallTop} InstallArch=${FinalInstallArch} Arch="${Arch}"
	rm -f ${InstallArch}/conf.make
	{ sed -e '/Install.*=/ $(if ${PREFIX}, s:^\(Install.*=\)${PREFIX}:override \1$${PREFIX}:',\
					       s:^:override :') \
	      -e '/INSTALL_PL=/ s:=.*:=$${InstallTop}/support/install.pl:' \
	      -e '/^Java/d' -e '/^JAVA/d' -e '/^JNI/d' -e '/^ANT=/d' \
	      -e '/DESTDIR=/ { s/^/override /; q; }' \
	      ${BuildDir}/conf.make; \
	  $(if ${PREFIX}, echo "PREFIX=${PREFIX}";) \
	} >${InstallArch}/conf.make
	chmod 444 ${InstallArch}/conf.make

install-arch-prep:
	$(call _CreateDir,${InstallArch})

install-shared : $(if $(filter y,${JavaBuild}), compile-java-jars)
	$(call _CreateDir,${InstallTop})
	$(call _InstallSubdir,perllib)
	$(foreach a, ${Apps}, $(call _InstallShared,apps/$a))
	@+$(call _MakeApps, install-src)
	$(if ${.svn},${CallPolymake} --no-config --script filter_rules -m ${InstallTop} ${Apps})
	[ ! -d java_build ] || $(call _InstallSubdir,java_build,-X de)
	[ ! -d java_build ] || $(call _InstallSubdirRenamed,external/jreality/lib,java_build/jreality/lib)
	$(call _InstallSubdir,povray)
	$(call _InstallSubdir,scripts)
	$(call _InstallSubdir,xml)
	$(call _InstallSubdir,support,$(foreach f,${InstHelpers},-X $f))

ifdef .svn

### unit tests

test : compile
	${CallPolymake} --script run_testcases --applications ${Apps}

### maintenance

tagsFLAGS = -R -e -f $@ --exclude=.svn --exclude='.\#*' --exclude='\#*' --exclude='*~'

TAGS:
	ctags ${tagsFLAGS} --language-force=c++ lib apps/*/{src,include}
RTAGS:
	ctags ${tagsFLAGS} --language-force=perl --exclude=testsuite \
			   perl perllib apps/*/{perllib,rules,scripts} scripts support/*.pl apps/*/testsuite/*/test*.pl

include support/export.make

endif  # .svn

### automatic part of documentation

docs:
	${CallPolymake} $(if ${RPM_BUILD_ROOT},--no-config) --script generate_docs ${InstallDoc} ${Apps}
	$(if ${.svn}, ${CallPolymake} --script doxygen ${InstallDoc}/PTL)

### cleanup

clean-bin:
	@+$(call _MakeApps, clean)

clean : clean-bin $(if $(filter y,${JavaBuild}),clean-java)
	rm -rf doc_build

distclean:
	rm -rf build* doc_build $(if ${.svn},java_build)


# Local Variables:
# mode: Makefile
# End:
